<?php

///////////////////////////////////////////////////////////////////////////////
// 
// JK_CAD
//
// This script gets a CAD quote from the Bank of Canada, formats it, and inserts it
//
///////////////////////////////////////////////////////////////////////////////

// get quote from Bank of Canada and parse
$xml = simplexml_load_file('http://www.bankofcanada.ca/stats/assets/rates_rss/closing/en_USD.xml');
// note: parameters passed to children() below are to enter into "cb" namespace;
// see: http://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names
// and: http://stackoverflow.com/questions/1893024/basic-simplexml-working-example
$cb_value = $xml->children()->item->children('cb', true)->statistics->exchangeRate->value;

// check value for styling, and round
$cb_value_float = round((float) $cb_value, 3);
$cb_value = (string) $cb_value_float;
$cb_value_class = (($cb_value_float >= 1) ? "HeaderTickerPos" : "HeaderTickerNeg");

// write formatted string
$out_array = array("<span class=\"", $cb_value_class, "\">", $cb_value, "</span>");
echo join($out_array);

?>